/**
 * @file ngine.cpp
 * @author Suk Robert Koh (srkoh87z@gmail.com)
 */

#include "ngine/server/ngineserver.h"

NGServer *NGineServer::server = 0;

/**
 * Default constructor for NGine class
 */
NGineServer::NGineServer()
{

}

/**
 * Destructor for NGine class
 */
NGineServer::~NGineServer()
{

}

/**
 * Initializes the NGine
 *
 * @param	int		Port number to be opened
 * @param	int		Maximum number of users to accept
 * @param	int		Other properties (currently not used)
 */
void NGineServer::init(int port, int maxUser, int property)
{
	if(server == 0)
		server = new NGTcpServer(port, maxUser);
}

/**
 * Gets the size of the message queue
 *
 * @return	int		Size of the message queue
 */
int NGineServer::getQueueSize()
{
	return server->getQueueSize();
}

/**
 * Gets the first message of the queue
 *
 * @return	NGMessage *		Pointer to the first message of the message queue
 */
NGMessage *NGineServer::getFirstMessage()
{
	return server->getFirstMessage();
}

/**
 * Sends message to a user
 *
 * @param	BYTE			Type of the message to be sent
 * @param	const char *	Pointer to the data to be sent
 * @param	DWORD			ID of a user to send message to
 */
void NGineServer::sendMessage(BYTE type, const char *message, DWORD userId)
{
	// create message object
	NGMessage *msg = new NGMessage();
	msg->setType(type);
	msg->setData(message);

	// send to user with userId
	server->sendMessage(msg, userId);
}

/**
 * Sends message to all users currently connected
 *
 * @param	BYTE			Type of the message to be sent
 * @param	const char *	Pointer to the data to be sent
 */
void NGineServer::sendMessageAll(BYTE type, const char *message)
{
	// create message object
	NGMessage *msg = new NGMessage();
	msg->setType(type);
	msg->setData(message);

	// send to all connected users
	server->sendMessageAll(msg);
}

/**
 * Sends message to all users currently connected except the one specified by excludeUserId
 *
 * @param	BYTE			Type of the message to be sent
 * @param	const char *	Pointer to the data to be sent
 * @param	DWORD			ID of a user to be excluded
 */
void NGineServer::sendMessageAll(BYTE type, const char *message, DWORD excludeUserId)
{
	// create message object
	NGMessage *msg = new NGMessage();
	msg->setType(type);
	msg->setData(message);

	// send to all connected users except user with excludeUserId
	server->sendMessageAll(msg, excludeUserId);
}
